package com.aranandroid.customview.text

import android.content.Context
import android.graphics.Rect
import androidx.appcompat.widget.AppCompatTextView
import android.text.TextUtils.TruncateAt
import android.util.AttributeSet

/**
 * Created by Administrator on 2016/3/17.
 * 实现跑马灯效果的TextView
 */
class MarqueeText : AppCompatTextView {
    constructor(context: Context?) : super(context!!) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(
        context!!, attrs
    ) {
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!, attrs, defStyleAttr
    ) {
    }

    //android:ellipsize="marquee" 重写此方法是为了让TextView具有跑马灯特性
    override fun setEllipsize(where: TruncateAt) {
        super.setEllipsize(TruncateAt.MARQUEE)
    }

    //android:focusable="true"
    //android:focusableInTouchMode="true"  把此方法返回值改成true就可以获得焦点
    override fun isFocused(): Boolean {
        return true
    }

    /**
     * 用于 EditText 存在时抢占焦点
     */
    override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        if (focused) {
            super.onFocusChanged(focused, direction, previouslyFocusedRect)
        }
    }

    /**
     * Window与Window间焦点发生改变时的回调
     * 解决 Dialog 抢占焦点问题
     *
     * @param hasWindowFocus
     */
    override fun onWindowFocusChanged(hasWindowFocus: Boolean) {
        if (hasWindowFocus) {
            super.onWindowFocusChanged(hasWindowFocus)
        }
    }
}